package com.tmall.tmall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tmall.tmall.mapper.ProductCollectMapper;
import com.tmall.tmall.mapper.ProductImageMapper;
import com.tmall.tmall.mapper.ProductMapper;
import com.tmall.tmall.pojo.*;
import com.tmall.tmall.service.ProductCollectService;
import com.tmall.tmall.utils.ThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductCollectServiceImpl implements ProductCollectService {
    @Autowired
    private ProductCollectMapper productCollectMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductImageMapper productImageMapper;

    @Override
    public Integer insert(Integer productId) {
        Collect collect = Collect.builder().uid(ThreadLocalUtils.getUid()).pid(productId).build();
        productCollectMapper.insert(collect);
        return collect.getId();
    }

    @Override
    public void delete(List<Integer> ids) {
        productCollectMapper.delete(ids);
    }

    @Override
    public PageRt page(Integer pageNum,Integer pageSize,Integer uid) {
        PageHelper.startPage(pageNum, pageSize);
        List<CollectRt> list = productCollectMapper.page(uid);
        for (CollectRt collectRt : list) {
            //获取收藏的商品名字
            String name = productMapper.getNameById(collectRt.getPid());
            //获取收藏商品的图片
            String img = productImageMapper.getMainByPid(collectRt.getPid());
            collectRt.setName(name);
            collectRt.setImg(img);
        }
        PageInfo<CollectRt> pageInfo = new PageInfo<>(list);
        return new PageRt(pageInfo.getTotal(), pageInfo.getList());
    }
}
